/*  
 * i-OSGi - Tunable Bundle Isolation for OSGi
 * Copyright (C) 2011  Sven Schulz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.iosgi.util.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author Sven Schulz
 */
public class Streams {

	private static final ExecutorService EXEC_SVC = Executors
			.newCachedThreadPool();

	public static Future<Void> drain(final InputStream source,
			final OutputStream target) {
		return EXEC_SVC.submit(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				try {
					try {
						byte[] buf = new byte[4096];
						int read;
						while ((read = source.read(buf)) != -1) {
							target.write(buf, 0, read);
						}
					} finally {
						target.flush();
						target.close();
					}
				} finally {
					source.close();
				}
				return null;
			}
		});
	}

	public static Future<Void> drain(final InputStream source, final File target)
			throws FileNotFoundException {
		return drain(source, new FileOutputStream(target));
	}

}